package org.springframework.samples.mvc.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author yg.huang
 * @version v1.0
 *          DATE  2016/11/24
 */
public class MoneyValidator implements ConstraintValidator<Money,Double> {
    private String moneyReg = "^\\d+(\\.\\d{1,2})?$";//表示金额的正则表达式
    private Pattern moneyPattern = Pattern.compile(moneyReg);
    @Override
    public void initialize(Money money) {

    }

    @Override
    public boolean isValid(Double aDouble, ConstraintValidatorContext constraintValidatorContext) {
        if (aDouble == null)
            return true;
        return moneyPattern.matcher(aDouble.toString()).matches()&&aDouble>0;
    }
}
